package com.permission.systempermission.comm.security;

import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class AuthenticationFailure implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        AuthenticationException exception
    ) throws IOException, ServletException {
        System.out.println("xxxsdfsdf");
        String localizedMessage = exception.getLocalizedMessage();
        HashMap result = new HashMap();
        result.put("code", -1);
        result.put("message", localizedMessage);
        String json = JSON.toJSONString(result);
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().println(json);
    }
}
